package com.dbpms.Interceptor;

import com.dbpms.pojo.User;
import com.dbpms.pojo.menuPower.menuPowerPackage;
import com.dbpms.pojo.menuPower.menupowerinfo;
import com.dbpms.service.IUserService;
import com.dbpms.until.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * 登录拦截器
 */

public class LoginInterceptor implements HandlerInterceptor {

    @Autowired
    private IUserService service;

    // 在请求处理之前调用 (Controller方法调用之前)
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("------------ 1.登录拦截器 前，还未执行拦截器命令 ------------");

        // 判断是否登录
        if (request.getSession().getAttribute(Constants.USER_SESSION)==null){
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>未登录");
            HttpSession session = request.getSession();
            session.setAttribute("alert_","danger");
            session.setAttribute("Interceptor_Message","用户未登录！");
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }else{
            System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>已登录");

            // 根据权限生成菜单
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute(Constants.USER_SESSION);

            // 全部菜单列表
            List<menupowerinfo> infos = service.getMenuPowerInfo(user.getUserPower());
            // 整理后的菜单
            List<menuPowerPackage> packagesInfos = new ArrayList<>();

            for (int i = 0; i < infos.size(); i++) {
                // 一级菜单
                menupowerinfo menupowerinfo = null;
                // 二级菜单
                List<menupowerinfo> forList = new ArrayList<>();
                for (int j = 0; j < infos.size(); j++) {
                    // 获得一级菜单
                    if (infos.get(i).getMenuPowerRootId() == 0 && menupowerinfo == null){
                        menupowerinfo = infos.get(i);
                    }
                    // 获得二级菜单
                    if (infos.get(j).getMenuPowerRootId() == infos.get(i).getMenuPowerId()){
                        forList.add(infos.get(j));
                    }
                }
                if (menupowerinfo != null){
                    menuPowerPackage _package = new menuPowerPackage();
                    _package.setInfo(menupowerinfo);
                    _package.setList(forList);

                    packagesInfos.add(_package);
                }
            }

            session.setAttribute("menupowerinfo",packagesInfos);
        }

        return true; // 返回true继续流程，返回false取消流程
    }
    // 请求处理之后进行调用，但在视图被渲染之前 (Controller方法调用之后)
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("============= 2.请求后 ---> 开始执行拦截器 ---> 渲染视图 =============");

    }
    // 在整个请求结束之后调用，也就是在DispatcherServlet渲染了对应的视图之后执行（主要用于进行资源清理工作）
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

        System.out.println("------------ 3.请求结束，开始清理 ------------");

    }
}
